<?php
declare(strict_types=1);

namespace App\Controller\Backend;

use App\Logic\AdminArticleLogic;
use Exception;
use Hyperf\HttpServer\Annotation\AutoController;
use Psr\Http\Message\ResponseInterface;

#[AutoController(prefix: '/article', server: 'backend')]
class ArticleController extends AbstractController
{
    /**
     * 文章列表
     * @return ResponseInterface
     */
    public function lists(): ResponseInterface
    {
        $input = $this->request->all();
        $data = AdminArticleLogic::listArticle($input);
        return $this->apiReturn(200, 'succ', $data);
    }

    /**
     * 文章详情
     * @return ResponseInterface
     */
    public function detail(): ResponseInterface
    {
        $id = (int)$this->request->input('id', 0);
        if (!$id) {
            return $this->apiReturn(400, '参数错误');
        }
        return $this->apiReturn(200, 'succ', AdminArticleLogic::detailArticle($id));
    }

    /**
     * 修改文章
     * @return ResponseInterface
     */
    public function edit(): ResponseInterface
    {
        $id = $this->request->input('id');
        $input = $this->request->all();
        if (!$id) {
            return $this->apiReturn(400, '参数错误');
        }
        if (!intval($input['catid'])) {
            return $this->apiReturn(400, '文章栏目必须选择');
        }
        if (empty($input['title'])) {
            return $this->apiReturn(400, '标题不能为空');
        }
        if (empty($input['author'])) {
            return $this->apiReturn(400, '请填写作者');
        }
        if (empty($input['content'])) {
            return $this->apiReturn(400, '内容不能为空');
        }
        $ret = AdminArticleLogic::editArticle($id, $input);
        if ($ret) {
            return $this->apiReturn(200, '修改成功');
        }
        return $this->apiReturn(400, '修改失败');
    }

    /**
     * 添加文章
     * @return ResponseInterface
     */
    public function add(): ResponseInterface
    {
        $input = $this->request->all();
        if (!intval($input['catid'])) {
            return $this->apiReturn(400, '文章栏目必须选择');
        }
        if (empty($input['title'])) {
            return $this->apiReturn(400, '标题不能为空');
        }
        if (empty($input['content'])) {
            return $this->apiReturn(400, '内容不能为空');
        }
        if (empty($input['author'])) {
            return $this->apiReturn(400, '请填写作者');
        }
        $ret = AdminArticleLogic::addArticle($input);
        if (!$ret) {
            return $this->apiReturn(400, '添加失败');
        }
        return $this->apiReturn(200, '添加成功');
    }

    /**
     * 删除文章
     * @return ResponseInterface
     */
    public function delete(): ResponseInterface
    {
        $id = $this->request->input('id', '');
        if (empty($id)) {
            return $this->apiReturn(400, '参数错误');
        }
        AdminArticleLogic::delArticle($id);
        return $this->apiReturn(200, '删除成功');
    }

    /**
     * 推荐/取消推荐
     * @return ResponseInterface
     */
    public function recommend(): ResponseInterface
    {
        $id = $this->request->input('id', '');
        if (empty($id)) {
            return $this->apiReturn(400, '参数错误');
        }
        $op = (int)$this->request->input('op', 1);
        AdminArticleLogic::recommend($id, $op == 0);
        return $this->apiReturn(200, '操作成功');
    }

    /**
     * 分类列表
     * @return ResponseInterface
     */
    public function category(): ResponseInterface
    {
        $keyBy = (int)$this->request->input('keyBy', 0);
        return $this->apiReturn(200, 'succ', AdminArticleLogic::listCategory((bool)$keyBy));
    }

    /**
     * 添加分类
     * @return ResponseInterface
     */
    public function addCategory(): ResponseInterface
    {
        $input = $this->request->all();
        if (empty($input['catname'])) {
            return $this->apiReturn(400, '分类名称不能为空');
        }
        try {
            AdminArticleLogic::addCategory($input);
            return $this->apiReturn(200, '添加成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 修改分类
     * @return ResponseInterface
     */
    public function editCategory(): ResponseInterface
    {
        $catId = (int)$this->request->input('id', 0);
        $input = $this->request->all();
        if (empty($input['catname'])) {
            return $this->apiReturn(400, '分类名称不能为空');
        }
        try {
            AdminArticleLogic::editCategory($catId, $input);
            return $this->apiReturn(200, '修改成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 删除分类
     * @return ResponseInterface
     */
    public function delCategory(): ResponseInterface
    {
        $catId = (int)$this->request->input('id', 0);
        if (!$catId) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            AdminArticleLogic::delCategory($catId);
            return $this->apiReturn(200, '删除成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 分类详情
     * @return ResponseInterface
     */
    public function detailCategory(): ResponseInterface
    {
        $catId = (int)$this->request->input('id', 0);
        if (!$catId) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            $category = AdminArticleLogic::detailCategory($catId);
            return $this->apiReturn(200, 'succ', $category);
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 快速修改分类
     * @return ResponseInterface
     */
    public function updateCategoryField(): ResponseInterface
    {
        $id = (int)$this->request->input('id');
        $field = $this->request->input('field', '');
        $value = (int)$this->request->input('value', 0);
        if (!$id || empty($field)) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            AdminArticleLogic::updateCategoryField($id, $field, $value);
            return $this->apiReturn(200, '修改成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 作者列表
     * @return ResponseInterface
     */
    public function listAuthor(): ResponseInterface
    {
        $input = $this->request->all();
        $data = AdminArticleLogic::listAuthor($input);
        return $this->apiReturn(200, 'succ', $data);
    }
}